package com.android.myj.db;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.Log;

import com.android.myj.model.User;

public class DataHelper {
	// 数据库名称
	private static String DB_NAME = "weibo.db";
	// 数据库版本
	private static int DB_VERSION = 1;
	private SQLiteDatabase db;
	private SqliteHelper dbHelper;
	private static DataHelper dataHelper;

	private DataHelper(Context context) {
		dbHelper = new SqliteHelper(context, DB_NAME, null, DB_VERSION);
		db = dbHelper.getWritableDatabase();
	}

	public static DataHelper getInstance(Context context) {
		if (dataHelper == null) {
			dataHelper = new DataHelper(context);
		}
		return dataHelper;
	}

	public void Close() {
		db.close();
		dbHelper.close();
	}

	// 获取users表中的UserID、Access Token、Access Secret的记录
	public List<User> GetUserList(Boolean isSimple) {
		List<User> userList = new ArrayList<User>();
		Cursor cursor = db.query(SqliteHelper.TB_NAME, null, null, null, null,
				null, User.ID + " DESC");
		cursor.moveToFirst();
		while (!cursor.isAfterLast() && (cursor.getString(1) != null)) {
			User user = new User();
			user.setId(cursor.getString(0));
			user.setUserId(cursor.getString(1));
			user.setToken(cursor.getString(2));
			user.setTokenSecret(cursor.getString(3));
			if (!isSimple && cursor.getBlob(5) != null) {
				user.setUserName(cursor.getString(4));
				ByteArrayInputStream stream = new ByteArrayInputStream(cursor
						.getBlob(5));
				Drawable icon = Drawable.createFromStream(stream, "image");

				user.setUserIcon(icon);
			}
			userList.add(user);
			cursor.moveToNext();
		}
		cursor.close();
		return userList;
	}

	// 判断users表中的是否包含某个UserID的记录
	public Boolean HaveUserInfo(String UserId) {
		Boolean b = false;
		Cursor cursor = db.query(SqliteHelper.TB_NAME, null, User.USERID
				+ "=?", new String[] { UserId }, null, null, null);
		b = cursor.moveToFirst();
		Log.e("HaveUserInfo", b.toString());
		cursor.close();
		return b;
	}

	// 更新users表的记录，根据UserId更新用户昵称和用户图标
	public int UpdateUserInfo(String userName, Bitmap userIcon, String UserId) {
		ContentValues values = new ContentValues();
		values.put(User.USERNAME, userName);
		// BLOB类型
		final ByteArrayOutputStream os = new ByteArrayOutputStream();
		// 将Bitmap压缩成PNG编码，质量为100%存储
		userIcon.compress(Bitmap.CompressFormat.PNG, 100, os);
		// 构造SQLite的Content对象，这里也可以使用raw
		values.put(User.USERICON, os.toByteArray());
		int id = db.update(SqliteHelper.TB_NAME, values,
				User.USERID + "=?", new String[] { UserId });
		Log.e("UpdateUserInfo2", id + "");
		return id;
	}

	// 更新users表的记录
	public int UpdateUserInfo(User user) {
		ContentValues values = new ContentValues();
		values.put(User.USERID, user.getUserId());
		values.put(User.TOKEN, user.getToken());
		values.put(User.TOKENSECRET, user.getTokenSecret());
		int id = db.update(SqliteHelper.TB_NAME, values, User.USERID + "="
				+ user.getUserId(), null);
		Log.e("UpdateUserInfo", id + "");
		return id;
	}

	// 添加users表的记录
	public Long SaveUserInfo(User user) {
		ContentValues values = new ContentValues();
		values.put(User.USERID, user.getUserId());
		values.put(User.TOKEN, user.getToken());
		values.put(User.TOKENSECRET, user.getTokenSecret());
		Long uid = db.insert(SqliteHelper.TB_NAME, User.ID, values);
		Log.e("SaveUserInfo", uid + "");
		return uid;
	}

	// 添加users表的记录
	public Long SaveUserInfo(User user, Bitmap userIcon) {
		ContentValues values = new ContentValues();
		values.put(User.USERID, user.getUserId());
		values.put(User.USERNAME, user.getUserName());
		values.put(User.TOKEN, user.getToken());
		values.put(User.TOKENSECRET, user.getTokenSecret());
		final ByteArrayOutputStream os = new ByteArrayOutputStream();
		// 将Bitmap压缩成PNG编码，质量为100%存储
		userIcon.compress(Bitmap.CompressFormat.PNG, 100, os);
		// 构造SQLite的Content对象，这里也可以使用raw
		values.put(User.USERICON, os.toByteArray());
		Long uid = db.insert(SqliteHelper.TB_NAME, User.ID, values);
		Log.e("SaveUserInfo", uid + "");
		return uid;
	}

	// 删除users表的记录
	public int DelUserInfo(String userId) {
		int id = db.delete(SqliteHelper.TB_NAME, User.USERID + "=?",
				new String[] { userId });
		Log.e("DelUserInfo", id + "");
		return id;
	}


	public static User getUserByName(String userName,
			List<User> userList) {
		User userInfo = null;
		int size = userList.size();
		for (int i = 0; i < size; i++) {
			if (userName.equals(userList.get(i).getUserName())) {
				userInfo = userList.get(i);
				break;
			}
		}
		return userInfo;
	}

}